package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.response.AccountBaseResponse;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.enums.InoutTypeEnum;
import com.niiwoo.civet.account.service.AccountDubboService;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 资金划拨账单详情
 * Created by xiaoyingping on 2018/11/7.
 */
@Service
public class FundTransferBillDetailStrategyImpl implements BillDetailStrategy {

    @Reference(version = "1.0.0")
    private AccountDubboService accountDubboService;

    @Override
    public String build(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response) {
        // 查询提现订单,获取到帐方式withdraw_type和bank_card_id
        if(!CollectionUtils.isEmpty(userBillFlowList)){
            NiiwooBillRespDTO niiwooBill = userBillFlowList.get(0);
            AccountBaseResponse source = accountDubboService.loadAccountById(niiwooBill.getAccountId());
            AccountBaseResponse target = accountDubboService.loadAccountById(niiwooBill.getRelationAccountId());
            StringBuilder sbd = new StringBuilder();
            if(InoutTypeEnum.OUT_TYPE.getType().equals(niiwooBill.getInoutType())){
                sbd.append("将").append(this.getAccountRoleDesc(source.getAccountRole())).append("余额划拨至").append(this.getAccountRoleDesc(target.getAccountRole()));
            }else {
                sbd.append("将").append(this.getAccountRoleDesc(target.getAccountRole())).append("余额划拨至").append(this.getAccountRoleDesc(source.getAccountRole()));
            }

            UserBillDetailResponse.AmountItem amountItem = response.new AmountItem();
            amountItem.setName("交易说明");
            amountItem.setAmountDesc(sbd.toString());
            response.getAmountItems().add(amountItem);
        }



        return JSON.toJSONString(response);
    }


    private String getAccountRoleDesc(Byte accountRole){
        String desc = "";
        if(Objects.nonNull(accountRole)){
            UserRoleEnum userRoleEnum = UserRoleEnum.enumOf(accountRole);
            if(UserRoleEnum.INVESTOR == userRoleEnum){
                desc = "投资账户";
            }else if(UserRoleEnum.BORROWERS == userRoleEnum){
                desc = "借款账户";
            }
        }

        return desc;
    }
}
